// 数据请求 wx.request
class httpRequest{
  constructor(){

  }
  static sendRrquest(url, method, data,_header){
    var status = true;
    var header = _header ? _header:{
      'content-type': 'application/x-www-form-urlencoded'
    };

    var promise = new Promise(function (resolve, reject) {
      wx.getNetworkType({
        success: function (res) {
          // 返回网络类型2g，3g，4g，wifi, none, unknown
          var networkType = res.networkType
          if (networkType == "none") {
            wx.hideLoading();
            //没有网络连接
            wx.showModal({
              title: '提示',
              content: '网络连接失败,请检查您的网络设置',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  //返回res.confirm为true时，表示用户点击确定按钮
                  console.log('表示用户点击确定按钮')

                }
              }
            })
            status = false;
          } else if (networkType == "unknown") {
            wx.hideLoading();
            //未知的网络类型
            wx.showModal({
              title: '提示',
              content: '未知的网络类型,请检查您的网络设置',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  //返回res.confirm为true时，表示用户点击确定按钮
                  console.log('表示用户点击确定按钮')
                }
              }
            })
            status = false;
          } else {
            wx.request({
              url: url,
              data: data,
              method: method,
              header: header,
              success: resolve,
              fail: reject
            })

          }
        }
      })
      return status
    });
    return promise;
  }
  http_get(url){
    return new Promise((resolve,reject)=>{
      httpRequest.sendRrquest(url, "get", null, {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + getApp().globalData.access_token,
        "openid": getApp().globalData.openid
      }).then(
        res=>{
          if (res.data.success){
            resolve(res.data.data);
          }else{
            reject(res.data);
          }
         
        }
      ).catch(err=>{
        reject(err);
      })
    })
  }
  http_post(url,data) {
    return new Promise((resolve, reject) => {
      httpRequest.sendRrquest(url, "post", data, {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + getApp().globalData.access_token,
        "openid": getApp().globalData.openid
      }).then(
        res => {
          if (res.data.success) {
            resolve(res.data.data);
          } else {
            reject(res.data);
          }
        }
      ).catch(err => {
        reject(err);
      })
    })
  }
  http_post_json(url, data) {
    return new Promise((resolve, reject) => {
      httpRequest.sendRrquest(url, "post", data, {
        'content-type': 'application/json',
        'Authorization': 'Bearer ' + getApp().globalData.access_token,
        "openid": getApp().globalData.openid
      }).then(
        res => {
          if (res.data.success) {
            resolve(res.data.data);
          } else {
            reject(res.data);
          }
        }
      ).catch(err => {
        reject(err);
      })
    })
  }
  http_put_json(url, data) {
    return new Promise((resolve, reject) => {
      httpRequest.sendRrquest(url, "put", data, {
        'content-type': 'application/json',
        'Authorization': 'Bearer ' + getApp().globalData.access_token,
        "openid": getApp().globalData.openid
      }).then(
        res => {
          if (res.data.success) {
            resolve(res.data.data);
          } else {
            reject(res.data);
          }
        }
      ).catch(err => {
        reject(err);
      })
    })
  }
  http_put(url,data) {
    return new Promise((resolve, reject) => {
      httpRequest.sendRrquest(url, "put", data, {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + getApp().globalData.access_token,
        "openid": getApp().globalData.openid
      }).then(
        res => {
          if (res.data.success) {
            resolve(res.data.data);
          } else {
            reject(res.data);
          }
        }
      ).catch(err => {
        reject(err);
      })
    })
  }
  http_delete(url) {
    return new Promise((resolve, reject) => {
      httpRequest.sendRrquest(url, "delete", null, {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + getApp().globalData.access_token,
        "openid": getApp().globalData.openid
      }).then(
        res => {
          if (res.data.success) {
            resolve(res.data.data);
          } else {
            reject(res.data);
          }
        }
      ).catch(err => {
        reject(err);
      })
    })
  }
}

export {httpRequest}